﻿Imports System.Data
Imports System.Data.SqlClient
Imports capaNegocios

Public Class secHorario
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnAgregarHorario_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAgregarHorario.Click
        Dim nivel As String
        Dim grado As String
        Dim seccion As String
        Dim url As String

        nivel = Me.ddlNivelAgregar.SelectedValue
        grado = Me.ddlGradoAgregar.SelectedValue
        seccion = Me.ddlSeccionAgregar.SelectedValue

        If existeAnioAcademico(Date.Now.Year.ToString, grado, seccion) Then
            Me.lblMensaje.Text = "Ya existe el horario."
        Else
            url = "secHorario_agregar.aspx?nivel=" + nivel.Trim() + "&grado=" + grado.Trim() + "&seccion=" + seccion.Trim()
            Response.Redirect(url)
        End If
    End Sub

    Protected Sub btnBuscarHorario_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBuscarHorario.Click
        Dim nivel As String
        Dim grado As String
        Dim seccion As String
        Dim url As String

        nivel = Me.ddlNivelBuscar.SelectedValue
        grado = Me.ddlGradoBuscar.SelectedValue
        seccion = Me.ddlSeccionBuscar.SelectedValue

        url = "secHorario_buscar.aspx?nivel=" + nivel.Trim() + "&grado=" + grado.Trim() + "&seccion=" + seccion.Trim()
        Response.Redirect(url)
    End Sub

    Private Function existeAnioAcademico(ByVal _anio As String, ByVal _grado As String, ByRef _seccion As String) As Boolean
        Dim oClsConexion As clsConexion = New clsConexion()
        Dim objConexion As SqlConnection = New SqlConnection()
        Dim objComando As SqlCommand = New SqlCommand()
        Dim objDataAdapter As SqlDataAdapter = New SqlDataAdapter()
        Dim objDataTable As DataTable = New DataTable()

        Try
            objConexion = oClsConexion.AbrirConexion()

            objComando.CommandType = CommandType.StoredProcedure
            objComando.CommandText = "USP_S_BUSCARANIOACADEMICO"

            objComando.Parameters.Add(New SqlParameter("@SECCION", SqlDbType.Char))
            objComando.Parameters.Add(New SqlParameter("@ANIO", SqlDbType.VarChar))
            objComando.Parameters.Add(New SqlParameter("@GRADO", SqlDbType.Char))

            objComando.Parameters("@SECCION").Value = _seccion
            objComando.Parameters("@ANIO").Value = _anio
            objComando.Parameters("@GRADO").Value = _grado

            objComando.Connection = objConexion
            objComando.ExecuteNonQuery()
            objDataAdapter.SelectCommand = objComando
            objDataAdapter.Fill(objDataTable)

            If objDataTable.Rows.Count > 0 Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Return False
            Throw ex
        End Try
    End Function
End Class